<?php

/**
 * sitemap actions.
 *
 * @package    master_directory
 * @subpackage sitemap
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class sitemapActions extends sfActions {
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */

	public function executeIndex(){

		/*
		 $objReader = new PHPExcel_Reader_Excel5();
		 $objReader->setReadDataOnly(true);
		 $objPHPExcel = $objReader->load( "C:\Users\Mary\Desktop\Book1.xls" );



		 $rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();

		 */

		/*
		 $array_data = array();
		 foreach($rowIterator as $row){
			$cellIterator = $row->getCellIterator();
			$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
			if(1 == $row->getRowIndex ()) continue;//skip first row
			$rowIndex = $row->getRowIndex ();
			$array_data[$rowIndex] = array('A'=>'name', 'B'=>'domain','C'=>'language','D'=>'fb_app_id');

			foreach ($cellIterator as $cell) {
			if('A' == $cell->getColumn()){
			$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
			} else if('B' == $cell->getColumn()){
			$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
			} else if('C' == $cell->getColumn()){
			$array_data[$rowIndex][$cell->getColumn()] = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'YYYY-MM-DD');
			} else if('D' == $cell->getColumn()){
			$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
			}
			}
			}
			print_r($array_data);
			*/

		/*$sheets  = $objPHPExcel->getAllSheets();

		$array_data = array();

		for ($i = 0; $i < count($sheets)-1; $i++){
		$sheet = $sheets[$i];
		foreach($rowIterator as $row){
		$rowIndex = $row->getRowIndex ();
		//$array_data[$rowIndex] = array('A'=>'', 'B'=>'','C'=>'','D'=>'');

		$cell = $sheet->getCell('A' . $rowIndex);
		$array_data[$i][$rowIndex]['A'] = $cell->getCalculatedValue();
		$cell = $sheet->getCell('B' . $rowIndex);
		$array_data[$i][$rowIndex]['B'] = $cell->getCalculatedValue();
		$cell = $sheet->getCell('C' . $rowIndex);
		$array_data[$i][$rowIndex]['C'] = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'YYYY-MM-DD');
		$cell = $sheet->getCell('D' . $rowIndex);
		$array_data[$i][$rowIndex]['D'] = $cell->getCalculatedValue();
		}
		}



		//setActiveSheetIndex(2);
		//$sheet = $objPHPExcel->getActiveSheet();


		echo '<pre>';
		print_r($array_data);
		echo '</pre>';


		exit();
		*/
		$posible_directories = Doctrine_Core::getTable ( 'MasterDirectory' )->retreiveActiveDirectories ();

		//$this->directories = array();
		$this->directories = $posible_directories;

		/*
		 foreach ($posible_directories as $directory){

			//$subfolder = strtolower(str_replace("-", "", substr($directory->getDomain(), 0, strpos($directory->getDomain(), "."))))  . '-sitemap';

			$subfolder = strtolower($directory->getDomain())  . '-sitemap';

			$sitemap_folder = sfConfig::get('sf_upload_dir') . '/../' . $subfolder;

			if (is_dir($sitemap_folder)){
			array_push($this->directories, $directory);
			}
			}*/

	}

	public  function executeGetTotalXml(sfRequest $request){

		$directory_id = $request->getParameter('directory_id');

		$total_sitemaps = 0;

		$search_template_id = null;
		$directory = Doctrine_Core::getTable('MasterDirectory')->find($directory_id);

		$existing_sitemap_counter = $directory->getSitemapCounter();

		if (count($existing_sitemap_counter) > 0){
			$total_sitemaps = $existing_sitemap_counter[0]->getSitemapsLeft();
		}
		else{
			$configurations = $directory->getSearchConfiguration();
			$directory_templates = Doctrine_Core::getTable('Template')->retreiveActiveTemplatesByDirectory($directory_id);
			$search_template_id = $configurations[0]->getTemplateId();
			$loaded_contents = array();

			foreach ($directory_templates as $directory_template) {
				//if($directory_template->getId() != $search_template_id || $directory->getSearchInSitemap() == true){
					$template_content_url = Doctrine_Core::getTable('TemplateContent')->getTemplateContent($directory_template->getId(), sfConfig::get('app_content_type_url'));
					$url_content = $template_content_url->getTheContent();

					$matchPattern = "/#%#(.*?)#%#/";

					$variables_name = array();
					$variables_str = array();

					if (preg_match_all ( $matchPattern, $url_content, $matches )) {
						$variables_name = $matches [1];
						$variables_str = $matches[0];
					}

					$base_array = array();
					//creating first array
					foreach ($variables_name as $variable_name) {
						$pieces = explode(".", $variable_name);
						$category = Doctrine_Core::getTable('Category')->retreiveActiveCategoryByNameDirectory($directory_id, $pieces[0]);
						if(array_key_exists($category->getId(), $base_array)){
							array_push($base_array[$category->getId()], $variable_name);
						}else{
							$variable_names = array();
							array_push($variable_names, $variable_name);
							$base_array[$category->getId()] = $variable_names;
						}
					}

					$contents_arr = array();

					//ordering variables from parent to son
					foreach ($base_array as $category_id => $variableNames) {
						usort($variableNames, array($this, 'parentFirst'));
						$base_array[$category_id] = $variableNames;
						$last_variable = $variableNames[count($variableNames) - 1];

						$template2level_ids = array();
						foreach($directory_template->getTemplate2Level() as $template2level){
							array_push($template2level_ids, $template2level->getId());
						}

						$level = $this->locateLevel($last_variable, $directory_template, $template2level_ids);
						$contents = null;
						if (!array_key_exists($level->getId(), $loaded_contents)){
							$the_table = Doctrine_Core::getTable ( 'Level' );
							$the_table->setTreePath();
							$the_table->findTreePath($level);
							$tree_path = $the_table->getTreePath();
							$category_content = Doctrine_Core::getTable('CategoryContent');
							$category_content->setContentArr();
							$contents = Doctrine_Core::getTable('CategoryContent')->getContentsByTreePath($tree_path, $level->getCategoryId());
							$loaded_contents[$level->getId()] = $contents;
						}else{
							$contents = $loaded_contents[$level->getId()];
						}
						//echo $directory_template->getName() . " | " . $last_variable  . " | " . count($contents). '<br>';

						array_push($contents_arr, $contents);

					}

					$url_count = 1;

					for ($i = 0; $i < count($contents_arr); $i++) {
						$current_contents_array = $contents_arr[$i];
						$url_count = $url_count * count($current_contents_array);
					}

					$total_sitemaps += $url_count;


			//	}
			}

			//echo $total_sitemaps; exit();

			$total_sitemaps = $total_sitemaps % 50000 == 0 ? $total_sitemaps / 50000 : intval($total_sitemaps / 50000) + 1;

			//create table instance
			$sitemap_counter = new SitemapCounter();
			$sitemap_counter->setDirectoryId($directory_id);
			$sitemap_counter->setSitemapsLeft($total_sitemaps);
			$sitemap_counter->setTotalSitemaps($total_sitemaps);
			$sitemap_counter->setLastTemplatePosition(0);
			$sitemap_counter->save();

		}

		$this->total_sitemaps = $total_sitemaps;



	}


	public function executeGenerateXml(sfRequest $request){

		$directory_id = $request->getParameter('directory_id');
		$search_template_id = null;
		$directory = Doctrine_Core::getTable('MasterDirectory')->find($directory_id);
		$configurations = $directory->getSearchConfiguration();
		$search_template_id = $configurations[0]->getTemplateId();

		$directory_templates = Doctrine_Core::getTable('Template')->retreiveActiveTemplatesByDirectory($directory_id);

		$existing_sitemap_counter = $directory->getSitemapCounter();

		$current_template_position = $existing_sitemap_counter[0]->getLastTemplatePosition();

		$template_count = count($directory_templates);

		$loaded_contents = array();

		$urls = array();

		$pushed_url = 0;

		$keep_going = true;

		$temp_x = array();
		foreach ($directory_templates as $x){
			array_push($temp_x, $x);
		}
		for ($k = $current_template_position; $k < $template_count; $k++){
			$directory_template = $temp_x[$k];
			//$the_value = $keep_going && ($directory_template->getId() != $search_template_id || $directory->getSearchInSitemap() == true) ;
			//if ($keep_going && ($directory_template->getId() != $search_template_id || $directory->getSearchInSitemap() == true) ){
			if ($keep_going ){

				$template2level_ids = array();
				$template2level_uppers = array();
				foreach($directory_template->getTemplate2Level() as $template2level){
					array_push($template2level_ids, $template2level->getId());
					$t2l_uppers = $template2level->getUpperLevel();
					foreach ($t2l_uppers as $t2l_upper) {
						array_push($template2level_uppers, $t2l_upper);
					}
				}
				$template_urls = array();

				$template_content_url = Doctrine_Core::getTable('TemplateContent')->getTemplateContent($directory_template->getId(), sfConfig::get('app_content_type_url'));
				$url_content = $template_content_url->getTheContent();

				$matchPattern = "/#%#(.*?)#%#/";

				$variables_name = array();
				$variables_str = array();

				if (preg_match_all ( $matchPattern, $url_content, $matches )) {
					$variables_name = $matches [1];
					$variables_str = $matches[0];
				}

				$base_array = array();
				//creating first array
				foreach ($variables_name as $variable_name) {
					$pieces = explode(".", $variable_name);
					$category = Doctrine_Core::getTable('Category')->retreiveActiveCategoryByNameDirectory($directory_id, $pieces[0]);
					if(array_key_exists($category->getId(), $base_array)){
						array_push($base_array[$category->getId()], $variable_name);
					}else{
						$variable_names = array();
						array_push($variable_names, $variable_name);
						$base_array[$category->getId()] = $variable_names;
					}
				}
					
				/*if ($directory_template->getId() == 98) {
				 echo 'tiene?<br>';
				 var_dump($base_array);
				 //echo $url_content;
				 exit();
				 }
				 else{
				 echo $directory_template->getId() . '<br>';
				 }*/
				$contents_arr = array();
				$contents_levels = array();
				$contents_indexes = array();

				//ordering variables from parent to son
				foreach ($base_array as $category_id => $variableNames) {
					usort($variableNames, array($this, 'parentFirst'));
					$base_array[$category_id] = $variableNames;
					$last_variable = $variableNames[count($variableNames) - 1];

					$level = $this->locateLevel($last_variable, $directory_template, $template2level_ids);

					array_push($contents_levels, $level->getId());

					$contents = null;
					if (!array_key_exists($level->getId(), $loaded_contents)){
						$the_table = Doctrine_Core::getTable ( 'Level' );
						$the_table->setTreePath();
						$the_table->findTreePath($level);
						$tree_path = $the_table->getTreePath();
						$category_content = Doctrine_Core::getTable('CategoryContent');
						$category_content->setContentArr();
						$contents = Doctrine_Core::getTable('CategoryContent')->getContentsByTreePath($tree_path, $level->getCategoryId());
						$loaded_contents[$level->getId()] = $contents;
					}else{
						$contents = $loaded_contents[$level->getId()];
					}
					//echo $directory_template->getName() . " | " . $last_variable  . " | " . count($contents). '<br>';

					array_push($contents_arr, $contents);

				}
					
				$urls[$directory_template->getId()] = array();
					
				for ($i = 0; $i < count($contents_arr); $i++) {
					$current_contents_array = $contents_arr[$i];
					$temp2level = Doctrine_Core::getTable('Template2Level')->getTemplatesLevelCombination($directory_template->getId(), $contents_levels[$i]);
					$last_used_content = $temp2level->getLastUsedContent();
					$last_used_content = count($last_used_content) > 0 ? $last_used_content[0] : null;
					$last_used_content_position = $last_used_content != null ? $last_used_content->getLastContentPosition() : 0;
					//$melcoching_position = $last_used_content != null ? $last_used_content->getMelcochingPosition() : 0;
					$upper_needed = false;
					$brother_needed = false;
					$brother_position = 0;
					if($i == 0){
						$last_used_content_position = 0;
						//TODO: cambiar foreach por for
						$previous_parent = null;
						$brothers = array();
						for ($h = $last_used_content_position; $h < count($current_contents_array); $h++){
							//$contents_indexes[$i]  = $h;
							$current_content_element = $current_contents_array[$h];
							$temp_url = $url_content;



							//main variable
							//$previous_parent = $current_content_element->getParent();
							//$previous_parent = $previous_parent != null && $previous_parent->getId() == $current_content_element->getParentId() ? $previous_parent : $current_content_element->getParent() ;
							$categoryVariableNames = $base_array[$current_content_element->getCategoryId()];
							$temp_url = str_replace("#%#" . $categoryVariableNames[count($categoryVariableNames)- 1] . "#%#", str_replace(" ", "-",  trim($current_content_element->getName())), $temp_url);
							if (count($categoryVariableNames) > 1){
								for ($j = count($categoryVariableNames) - 1; $j >= 0 ; $j--) {
									$variable = $categoryVariableNames[$j];
									//$upper_level = Doctrine_Core::getTable('UpperLevel')->getUpperLevelByName($template2level_ids, $variable);


									if($h == $last_used_content_position){
										$upper_level = $this->getUpper($template2level_uppers, $variable);
										if($upper_level != null){
											$upper_needed = true;
										}
										else{
											$found_side_level = Doctrine_Core::getTable('SideLevel')->getSideLevelByName($template2level_ids, $variable);
											if($found_side_level != null){
												$brother_position = $found_side_level->getPosition();
												$brother_needed = true;
											}
										}
									}

									$upper_level = $this->getUpper($template2level_uppers, $variable);
									if($upper_needed){
										//if($upper_level != null){
										$previous_parent = $previous_parent != null && $previous_parent->getId() == $current_content_element->getParentId() ? $previous_parent : $current_content_element->getParent() ;
										//parent
										$temp_url = str_replace("#%#" . $variable . "#%#",  str_replace(" ", "-",  trim($previous_parent->getName())), $temp_url);
										if (count($categoryVariableNames) > 2){
											$previous_parent = $previous_parent->getParent();
										}
											
									}else if ($brother_needed){
										//brother
										//$found_side_level = Doctrine_Core::getTable('SideLevel')->getSideLevelByName($template2level_ids, $variable);
										//if($found_side_level != null){
										if($previous_parent == null || $previous_parent->getId() != $current_content_element->getParentId()){
											$parent = $current_content_element->getParent();
											$brothers = $parent->getCategoryContent();
										}
										//$parent = $previous_parent != null && $previous_parent->getId() == $current_content_element->getParentId() ? $previous_parent : $current_content_element->getParent() ;
										//$parent = $current_content_element->getParent();
										//$brothers = $parent->getLevel();
										$temp_url = str_replace("#%#".$variable."#%#", str_replace(" ", "-",  trim($brothers[$brother_position])) , $variable);
										//}
									}
								}
							}
							array_push($urls[$directory_template->getId()], $temp_url);
							if (count($contents_arr) == 1){
								//echo 'pusho en el single<br>';
								$pushed_url++;
							}
						}

						/*					echo '<pre>';
						 print_r($template_urls);
						 echo '</pre>';
						 exit();
						 */
					}else{
						//echo 'entro en el else <br>';

							
						//echo count($urls[$directory_template->getId()]) . '<br>';
							
						//echo 'count($contents_arr) ' . count($contents_arr) . '<br>';
						$temp_url_arr = array();

						$temp2level_0 = Doctrine_Core::getTable('Template2Level')->getTemplatesLevelCombination($directory_template->getId(), $contents_levels[0]);
						$last_used_content_0 = $temp2level_0->getLastUsedContent();
						$last_used_content_0 = count($last_used_content_0) > 0 ? $last_used_content_0[0] : null;
						$last_used_content_position_0 = $last_used_content_0 != null ? $last_used_content_0->getLastContentPosition() : 0;
							
						$melcoching_position = $last_used_content != null ? $last_used_content->getMelcochingPosition() : 0;
						//echo '<br>'.$current_contents_array[0]->getName(); exit();
							
						//foreach ($urls[$directory_template->getId()] as $temp_url) {
						for($w = $melcoching_position; $w < count($urls[$directory_template->getId()]); $w++){
							$w_arr = $urls[$directory_template->getId()];
							$temp_url = $w_arr[$w];
							//	$contents_indexes[0]  = $w;

							//TODO: cambiar foreach por for
							//foreach ($current_contents_array as $current_content_element) {
							$previous_parent = null;
							$brother_position = 0;
							$brother_needed = false;
							for ($h = $last_used_content_position; $h < count($current_contents_array); $h++){
									
								$contents_indexes[$i]  = $h;
								$current_content_element = $current_contents_array[$h];
								if ($pushed_url < 50000){
									$temp_url_temp = $temp_url;
									//main variable
									//$previous_parent = $previous_parent != null && $previous_parent->getId() == $current_content_element->getParentId() ? $previous_parent : $current_content_element->getParent() ;
									$categoryVariableNames = $base_array[$current_content_element->getCategoryId()];
									$temp_url_temp = str_replace("#%#" . $categoryVariableNames[count($categoryVariableNames)- 1] . "#%#", str_replace(" ", "-",  trim($current_content_element->getName())), $temp_url_temp);
									if (count($categoryVariableNames) > 1){
										for ($j = count($categoryVariableNames) - 1; $j >= 0 ; $j--) {
											$variable = $categoryVariableNames[$j];

											if($h == $last_used_content_position){
												$upper_level = $this->getUpper($template2level_uppers, $variable);
												if($upper_level != null){
													$upper_needed = true;
												}else{
													$found_side_level = Doctrine_Core::getTable('SideLevel')->getSideLevelByName($template2level_ids, $variable);
													if($found_side_level != null){
														$brother_position = $found_side_level->getPosition();
														$brother_needed = true;
													}
												}
													
											}


											//$upper_level = Doctrine_Core::getTable('UpperLevel')->getUpperLevelByName($template2level_ids, $variable);
											$upper_level = $this->getUpper($template2level_uppers, $variable);
											//if($upper_level != null){
											if($upper_needed){
												$previous_parent = $previous_parent != null && $previous_parent->getId() == $current_content_element->getParentId() ? $previous_parent : $current_content_element->getParent() ;
												//parent
												$temp_url_temp = str_replace("#%#" . $variable . "#%#",  str_replace(" ", "-",  trim($previous_parent->getName())), $temp_url_temp);
												if (count($categoryVariableNames) > 2){
													$previous_parent = $previous_parent->getParent();
												}
											}else if ($brother_needed){
												//brother
												//$found_side_level = Doctrine_Core::getTable('SideLevel')->getSideLevelByName($template2level_ids, $variable);
												//if($found_side_level != null){
												if($previous_parent == null || $previous_parent->getId() != $current_content_element->getParentId()){
													$parent = $current_content_element->getParent();
													$brothers = $parent->getCategoryContent();
												}
												//$parent = $previous_parent != null && $previous_parent->getId() == $current_content_element->getParentId() ? $previous_parent : $current_content_element->getParent() ;
												//$parent = $current_content_element->getParent();
												//$brothers = $parent->getLevel();
												$temp_url = str_replace("#%#".$variable."#%#", str_replace(" ", "-",  trim($brothers[$brother_position])) , $variable);
												//}
											}
										}
									}
									array_push($temp_url_arr, $temp_url_temp);

									if ($i == count($contents_arr) - 1){
										$pushed_url++;
										/*if ($pushed_url == 50000){
										 echo 'igual igual 500000 !!!!!!<br>';
										 echo 'la ultima h ' . count($current_contents_array) . '<br>';
										 echo 'la h: ' . $h . '<br>' . 'keep going ' . $keep_going;
										 exit();

										 //break;
										 }	*/
									}

								}
								else if($keep_going){

									$keep_going = false;

									//echo 'entre <br>';

									$existing_sitemap_counter[0]->setLastTemplatePosition($k);
									$existing_sitemap_counter[0]->save();

									//TODO UPDATE POINTERS TABLES
									/*if ($last_used_content == null){
									 	
									foreach ($directory_templates as $directory_x) {
									$main_selections = Doctrine_Core::getTable('Template2Level')->getTemplatesLevelsByTemplateId($directory_x->getId());
									foreach ($main_selections as $main_selection) {
									$used_content = $main_selection->getLastUsedContent();
									if($used_content != null){
									$used_content->delete();
									}
									}
									}

									$last_used_content = new LastUsedContent();
									}*/
									foreach ($directory_templates as $directory_x) {
										$main_selections = Doctrine_Core::getTable('Template2Level')->getTemplatesLevelsByTemplateId($directory_x->getId());
										foreach ($main_selections as $main_selection) {
											$used_content = $main_selection->getLastUsedContent();
											if($used_content != null){
												$used_content->delete();
											}
										}
									}
									//	var_dump($contents_indexes); exit();

									//for ($l = 0; $l < count($contents_indexes); $l++) {

									/*echo 'template: ' . $directory_template->getName() . "<br>";
									 echo 'melcoching count: ' . count($urls[$directory_template->getId()]) . "<br>";
									 echo 'starting melcoching position: ' . $melcoching_position . "<br>";
									 echo 'content position: ' . $h . "<br>";
									 echo 'next melcoching: ' . $w . "<br>";
									 */

									//exit();
									//$specific_content_array = $contents_arr[count($contents_indexes)-1];

									//echo 'count = ' . count($specific_content_array) . '  index = ' . $contents_indexes[$l]. '<br>';
									/*if ($l == 0 && $contents_indexes[$l] > count($specific_content_array)){
										$contents_indexes[$l] = $contents_indexes[$l] - count($specific_content_array);
										}*/
									$temp2level_x = Doctrine_Core::getTable('Template2Level')->getTemplatesLevelCombination($directory_template->getId(), $contents_levels[$i]);
									$last_used_content_x = new LastUsedContent();
									$last_used_content_x->setTemplate2levelId($temp2level_x->getId());

									$last_used_content_x->setMelcochingPosition($w);


									//$last_used_content_x->setCategoryContentId($specific_content_array[$contents_indexes[$l]]);
									$last_used_content_x->setCategoryContentId($current_content_element->getId());
									$last_used_content_x->setLastContentPosition($contents_indexes[$i]);
									$last_used_content_x->save();
									//}

									/*
									 $last_used_content->setTemplate2levelId($temp2level->getId());
									 $last_used_content->setCategoryContentId($current_content_element->getId());
									 $last_used_content->setLastContentPosition($h);
									 $last_used_content->save();
									 */
									break;
								}

								/*if ($pushed_url == 50000){
								 break;
								 }*/
									
							}

							$last_used_content_position = 0;

							if ($pushed_url == 50000){
								//echo 'igual igual 500000 <br>';
								if (!$keep_going){
									break;
								}
								//break;
								//$keep_going = false;
							}

						}
							
						//$template_urls = $temp_url_arr;
						$urls[$directory_template->getId()] = $temp_url_arr;
							
					}
				}
					
			}else if (!$keep_going){
				//echo 'not keep going';
				break;
			}
		}

		//$this->writeXml($urls, $existing_sitemap_counter[0]);
		$this->insertIntoDb($urls, $existing_sitemap_counter[0]);

		$this->total_sitemaps = $existing_sitemap_counter[0]->getSitemapsLeft();

		$this->setTemplate("getTotalXml");
		//	echo 'before exit';
		//	exit();

	}

	protected function insertIntoDb($urls, $sitemap_counter){

		$directory_id = $sitemap_counter->getDirectoryId();

		$original_sitemap_left = $sitemap_counter->getSitemapsLeft();

		$directory =  Doctrine_Core::getTable('MasterDirectory')-> find($directory_id);

		$sitemap_counter->setSitemapsLeft($sitemap_counter->getSitemapsLeft() - 1);

		$sitemap_counter->save();

		$directory_domain = strtolower($directory->getDomain());

		$url_keys = array_keys($urls);

		foreach ($url_keys as $url_key){
			$template_urls = $urls[$url_key];
			foreach ($template_urls as $template_url){
				$domain = 'http://' . $directory_domain  . '/';
				$content_url = str_replace(' ', '-', DirectoryUtils::slugify($template_url) );
				$ext = '.html';
				$url = strtolower($domain .$content_url . $ext );
				//$str_put = sprintf($format, $url);

				$url_db = new Urls();
				$url_db->setUrl($url);
				$url_db->setDirectoryId($directory_id);
				$url_db->save();
			}
		}

	}


	protected function writeXml($urls, $sitemap_counter){
		$directory_id = $sitemap_counter->getDirectoryId();

		$original_sitemap_left = $sitemap_counter->getSitemapsLeft();

		$directory =  Doctrine_Core::getTable('MasterDirectory')-> find($directory_id);

		$sitemap_counter->setSitemapsLeft($sitemap_counter->getSitemapsLeft() - 1);

		$sitemap_counter->save();

		$directory_domain = strtolower($directory->getDomain());

		//$subfolder = strtolower(str_replace("-", "", str_replace(".", "_", $directory->getDomain()) ))  . '-sitemap';
		$subfolder = strtolower($directory->getDomain())  . '-sitemap';

		$sitemap_folder = sfConfig::get('sf_upload_dir') . '/../' . $subfolder;



		$file_counter = intval($sitemap_counter->getTotalSitemaps() - $sitemap_counter->getSitemapsLeft());

		$url_keys = array_keys($urls);

		/*if (!is_dir(sfConfig::get('sf_upload_dir') . '/xml/' . $directory_domain)){
			mkdir(sfConfig::get('sf_upload_dir') . '/xml/' . $directory_domain);
			}*/



		//$index_file = sfConfig::get('sf_upload_dir') . '/xml/' . $directory_domain  .'/sitemap_index.xml' ;

		$index_file = $sitemap_folder .'/sitemap_index.xml' ;

		$index_handle = fopen($index_file,"w+") or die ("unable to open file: " . $index_file);


		$index_header = '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

		fwrite($index_handle, $index_header);

		$index_str = '<sitemap> <loc>http://' . $directory_domain . '/' . $subfolder . '/%s</loc>  <lastmod>%s</lastmod> </sitemap>';

		for($i = 1; $i <= $file_counter; $i++){
			fwrite($index_handle, sprintf($index_str, 'sitemap_'.$i.'.xml', date('c')));
		}
		$index_close = '</sitemapindex>';
		fwrite($index_handle, $index_close);
		fclose($index_handle);




		//$file = sfConfig::get('sf_upload_dir') . '/xml/' . $directory_domain  .'/sitemap_'. $file_counter . '.xml' ;

		$file = $sitemap_folder  .'/sitemap_'. $file_counter . '.xml' ;


		$handle = fopen($file,"w+") or die ("unable to open file: " . $file);

		$header = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
		fwrite($handle, $header);

		if ($sitemap_counter->getTotalSitemaps() == $original_sitemap_left){
			$home = ' <url> <loc>http://' . $directory_domain . '</loc>  <changefreq>monthly</changefreq>  <priority>1.00</priority>  </url>';

			fwrite($handle, $home);
		}

		$format = ' <url> <loc>%s</loc> <changefreq>monthly</changefreq>  <priority>0.60</priority> </url>';


		foreach ($url_keys as $url_key){
			$template_urls = $urls[$url_key];
			foreach ($template_urls as $template_url){
				$domain = 'http://' . $directory_domain  . '/';
				$content_url = str_replace(' ', '-', DirectoryUtils::slugify($template_url) );
				$ext = '.html';
				$url = strtolower($domain .$content_url . $ext );
				$str_put = sprintf($format, $url);


				fwrite($handle, $str_put);
			}
		}
		fwrite($handle, '</urlset>');
		fclose($handle);



		$robot_file = $sitemap_folder  . '/robots.txt';
		$robot_handle = fopen($robot_file,"w+") or die ("unable to open file: " . $robot_file) ;

		$index_url = "http://" . $directory_domain  . '/' . $subfolder . "/sitemap_index.xml";

		fwrite($robot_handle, $index_url);

		fclose($robot_handle);
	}



	protected function locateLevel($main_variable_name, $template, $template2level_ids){
		//look for the variable in template2level
		$level = null;
		$templates2level = Doctrine_Core::getTable('Template2Level')->getTemplate2LevelByName($template->getId(), $main_variable_name);
		if ($templates2level != null){
			$level = $templates2level->getLevel();
		}
		else {
			//look for the variable in upper_level
			$upper_level = Doctrine_Core::getTable ( 'UpperLevel' )->getUpperLevelByName($template2level_ids, $main_variable_name);
			if ($upper_level != null){
				$level = $upper_level->getLevel();
			}
			else{
				$level = Doctrine_Core::getTable('SideLevel')->getSideLevelByName($template2level_ids, $main_variable_name)->getLevel();
			}
		}
		return $level;
	}

	private function parentFirst($variable1, $variable2) {
		$pieces1 = explode(".", $variable1);
		$pieces2 = explode(".", $variable2);
		return count($pieces2) - count($pieces1);
	}

	private function getUpper($template2level_uppers, $variable_name) {
		$upper = null;
		$upper_counter = 0;
		while ($upper == null && $upper_counter < count($template2level_uppers)) {
			$current_upper = $template2level_uppers[$upper_counter];
			if($current_upper->getName() == $variable_name){
				$upper = $current_upper;
			}
			$upper_counter++;
		}
		return $upper;
	}

}
